/**
 * Description:
 * User:岁亮亮
 * Date:2025-03-11
 * TIME:8:32
 */
public class SearchTree {

    static class TreeNode{
        int value;
        TreeNode left;
        TreeNode right;
        TreeNode(int value){
            this.value = value;
        }
    }
    TreeNode root = null;
    int size = 0;
    public void print(){
        printReal(root);
        System.out.println();
    }

    private void printReal(TreeNode root){
        if(root == null){
            return;
        }
        printReal(root.left);
        System.out.print(root.value + " ");
        printReal(root.right);

    }

    public Boolean insert(int value){
        TreeNode newNode  = new TreeNode(value);
        if(root == null){
            root = newNode;
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;
        while(cur != null){
            if(cur.value < value){
                parent = cur;
                cur = cur.right;
            }
            else if(cur.value > value){
                parent = cur;
                cur = cur.left;
            }
            else {
                return false;
            }
        }
        if(parent.value < value){
            parent.right = newNode;
        }
        else {
            parent.left = newNode;
        }
        return true;

    }

    public boolean search(int value){
        TreeNode cur = root;
        while(cur != null){
            if(cur.value < value){
                cur = cur.right;
            }
            else if(cur.value > value){
                cur = cur.left;
            }
            else{
                return true;
            }
        }
        return false;
    }
    public boolean remove(int value){
        TreeNode cur = root;
        TreeNode parent = cur;
        while(cur != null){
            if(cur.value > value){
                parent =cur;
                cur = cur.left;
            }
            else if( cur.value < value){
                parent = cur;
                cur = cur.right;
            }
            else{
                break;
            }
        }
        if(cur == null)return false;
        if(cur.left == null){
            if(cur == root){
                root = root.right;
            }
            else if(parent.left == cur){
                parent.left = cur.right;
            }
            else if(parent.right == cur){
                parent.right = cur.right;
            }
        }
        else if(cur.right == null){
            if(cur == root){
                root = root.left;
            }
            else if(parent.left == cur){
                parent.left = cur.left;
            }
            else if(parent.right == cur){
                parent.right = cur.left;
            }
        }
        else{
            TreeNode target = cur.right;
            TreeNode targetParent = cur;
            while(target.left != null){
                targetParent = target;
                target = target.left;
            }
            if(targetParent.left == target){
                targetParent.left = target.right;
            }
            else{
                targetParent.right = target.right;
            }
            cur.value = target.value;
        }
        return true;

    }

}
